using System;
using System.Collections;
using System.Collections.Generic;
using TEngine;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace GameLogic
{
    // Gizmos管理器单例
    public class GroundGizmosManager : SingletonBehaviour<GroundGizmosManager>
    {
        [SerializeField]
        private List<Ground> activeGrounds = new List<Ground>();

        public void RegisterGround(Ground ground)
        {
            if (!activeGrounds.Contains(ground))
            {
                activeGrounds.Add(ground);
                Debug.Log($"注册地块: {ground.actuallytileDetails.girdX}, {ground.actuallytileDetails.gridY}");
            }
        }

        public void UnregisterGround(Ground ground)
        {
            if (activeGrounds.Contains(ground))
            {
                activeGrounds.Remove(ground);
                Debug.Log($"注销地块: {ground.actuallytileDetails.girdX}, {ground.actuallytileDetails.gridY}");
            }
        }

#if UNITY_EDITOR
        private void OnDrawGizmos()
        {
            if (activeGrounds == null || activeGrounds.Count == 0)
                return;

            foreach (var ground in activeGrounds)
            {
                if (ground == null || ground.actuallytileDetails == null || !ground.actuallytileDetails.isVisible)
                    continue;

                DrawGroundGizmos(ground);
            }
        }

        private void DrawGroundGizmos(Ground ground)
        {
            Vector3 position = new Vector3(ground.actuallytileDetails.girdX + 0.5f, ground.actuallytileDetails.gridY + 0.5f, 0);

            //// 绘制半透明背景
            //Gizmos.color = new Color(0, 0, 0, 0.8f);
            //Gizmos.DrawCube(position, new Vector3(0.9f, 0.7f, 0.01f));

            // 绘制边框（根据状态不同颜色）
            Gizmos.color = GetTileBorderColor(ground);
            Gizmos.DrawWireCube(position, new Vector3(0.92f, 0.72f, 0.01f));

            // 绘制文本信息
            DrawTextInfo(position, ground);
        }

        private Color GetTileBorderColor(Ground ground)
        {
            if (!ground.actuallytileDetails.canDig)
                return Color.red; // 已挖掘 - 红色
            else if (ground.actuallytileDetails.Wetness > 5)
                return Color.blue; // 湿润 - 蓝色
            else if (ground.actuallytileDetails.Fertility > 5)
                return Color.green; // 肥沃 - 绿色
            else
                return Color.white; // 普通 - 白色
        }

        private void DrawTextInfo(Vector3 position, Ground ground)
        {
            GUIStyle style = new GUIStyle();
            style.normal.textColor = Color.yellow;
            style.alignment = TextAnchor.MiddleCenter;
            style.fontSize = 8;
            style.fontStyle = FontStyle.Bold;

            string displayText = BuildDisplayText(ground);
            Handles.Label(position, displayText, style);
        }

        private string BuildDisplayText(Ground ground)
        {
            return $"({ground.actuallytileDetails.girdX},{ground.actuallytileDetails.gridY})\n" +
                   $"F:{ground.actuallytileDetails.Fertility} W:{ground.actuallytileDetails.Wetness}\n" +
                   $"D:{ground.actuallytileDetails.sinceDuged}";
        }
#endif
    }
}

